package com.example.demo.controller;

import com.example.demo.common.domain.Employee;
import com.example.demo.common.domain.Record;
import com.example.demo.common.util.Result;
import com.example.demo.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@Controller
public class DakaController {
    @Autowired
    @Qualifier("recordServiceImpl")
    private RecordService recordService;

    public boolean check(HttpSession session){
        if(session.getAttribute("employee")!=null)
            return true;
        else
            return false;
    }


    public boolean checkSb(Record r){//检查是否进行了打卡上班
        if(r == null || r.getRstarttime() == null){
            return false;
        }else{
            return true;
        }
    }


    public boolean checkXb(Record r){//检查是否进行了打卡下班
        if(r == null || r.getRendtime() == null){
            return false;
        }else{
            return true;
        }
    }
    @RequestMapping("/daka")
    public String daka(HttpSession session, Model model){//根据SESSION中的用户ID，搜索该用户今天的打卡记录，结果渲染到前端
        String start,end;
        boolean isStart = false;
        boolean isEnd = false;
        if(check(session)){
            Employee employee = (Employee) session.getAttribute("employee");
            model.addAttribute("employee",employee);
            Record r = recordService.selectByEidAndDate(employee.getEid(),new Date());


            if(checkSb(r)){
                start = r.getRstarttime().toString();
                isStart = true;
                if(checkXb(r)){
                    end = r.getRendtime().toString();
                    isEnd = true;
                }else{
                    end = "";
                }
            }else{
                start = "";
                end = "";
            }
            model.addAttribute("start",start);
            model.addAttribute("end",end);
            model.addAttribute("ifSb",isStart);
            model.addAttribute("ifXb",isEnd);
            return "daka";
        }else{
            return "login";
        }

    }

    @RequestMapping("/dakasb")
    @ResponseBody
    public Map shangban(HttpSession session){//根据SESSION中的用户ID产生一条新的打卡记录，设置打卡上班字段为当前时间，存入数据库中
        Date now = new Date();
        String s = now.toString();
        Employee e = (Employee)session.getAttribute("employee");
        if(e != null){
            int eid = e.getEid();
            Record r = new Record();
            r.setRstarttime(now);
            r.setEid(eid);
            r.setIsabsent((short)0);
            r.setRdate(now);
            recordService.insertSelective(r);
        }

        return Result.success(true).put("date",s);
    }

    @RequestMapping("/dakaxb")
    @ResponseBody
    public Map xiaban(HttpSession session){//根据SEESION中的用户ID搜索今日的打卡记录，设置打卡下班字段为当前时间，并更新数据库
        Date now = new Date();
        String s = now.toString();
        Employee e = (Employee)session.getAttribute("employee");


        if(e != null){
            int eid = e.getEid();
            recordService.updateXBTime(eid,now);
        }
        return Result.success(true).put("date",s);
    }
}
